jQuery(function(event) {

	$('#locationsList a.delBoard').click(function(e){
		if(!confirm('ARE_YOU_SURE'.translate()))
		{
			return;
		}
		 var form = $('#delSystemBoardForm');
		 form.find('input[name="code"]').val($(this).parents('tr').children('td').eq(1).text());
		 form.submit();
	});
	
	$('#locationsList input[type="button"].butNew').click(function(e){
		if(!confirm('ARE_YOU_SURE'.translate()))
		{
			return;
		}
		var tr = $(this).parents('tr');
		var iNewSysBoardCode = tr.find('input[name="newSysBoardCode"]');
		var iNewSysBoardTranslation = tr.find('input[name="newSysBoardTranslation"]');
		if(iNewSysBoardCode.val().replace(/\s+/, '').length == 0)
		{
			alert('PLEASE_ENTER_CODE'.translate());
			return;
		}
		if(iNewSysBoardTranslation.val().replace(/\s+/, '').length == 0)
		{
			alert('PLEASE_ENTER_TRANSLATION'.translate());
			return;
		}
		 var form = $('#newSystemBoardForm');
		 form.find('input[name="code"]').val(iNewSysBoardCode.val());
		 form.find('input[name="translation"]').val(iNewSysBoardTranslation.val());
		 form.submit();
	});

	$('#locationsList input[type="button"].butTranslate').click(function(e){
		var butTranslate = $(this);
		var tr = butTranslate.parents('tr');
		var code = tr.children('td').eq(1).text();
		var tdTranslation = tr.children('td').eq(2);
		var oldTranslation = tdTranslation.text();
		if (butTranslate.siblings('input.btnTranslateSubmit, input.btnTranslateCancel').length <= 0) {
			butTranslate.after('<input type="button" class="button-cancel btnTranslateCancel" value="' + 'BTN_CANCEL'.translate() + '" />');
			butTranslate.after('<input type="button" class="button-ok btnTranslateSubmit" value="' + 'BTN_OK'.translate() + '" />');
		} else {
			butTranslate.siblings('input.btnTranslateSubmit, input.btnTranslateCancel').show();
		}
		butTranslate.siblings('input.btnTranslateSubmit').one('click', function() { funSaveTranslation(); });
		butTranslate.siblings('input.btnTranslateCancel').one('click', function() { funCancelTranslation(); });
		butTranslate.hide();
		
		tdTranslation.text('');
		var inp = $('<input type="text" />')
			.val(oldTranslation)
			.keydown(function(event){
				switch(event.keyCode){
					case 13:
						funSaveTranslation();
						break;
					case 27:
						funCancelTranslation();
						break;
				}
			})
			.appendTo(tdTranslation)
		;
		inp.focus();
		
		var funSaveTranslation = function(e){
			var newVal = inp.val();
			if(newVal.replace(/\s+/, '').length == 0)
			{
				return;
			}
			$.ajax({
				url: '/a_admin/board.saveTranslation',
				method: 'post',
				timeout: 20000,
				async: true,
				data: {
					code: code,
					translation: newVal
				},
				success: function(data){
					if(data == 'success')
					{
						tdTranslation.removeAttr('style').text(newVal);
						funCloseTranslation();
					}
					else
					{
						alert('Translation saving error!');
					}							
				},
				error: function(){
					alert('Translation saving error!');
				}
			});
		};
		
		var funCloseTranslation = function(){
			butTranslate.show();
			butTranslate.siblings('input.btnTranslateSubmit, input.btnTranslateCancel').hide().unbind('click');
		};
		var funCancelTranslation = function(){
			tdTranslation.text(oldTranslation);
			funCloseTranslation();
		};
	});

	$('#locationsList input[type="button"].butChange').click(function(e){
		var but = $(this);
		tdBut = but.parent();
		var tr = but.parents('tr');
		var tds = tr.children('td');
		
		var vendorBoardCode = tds.eq(1).text();
		
		var tdSystemBoardCode = tds.eq(2);
		var oldSystemBoardCode = tdSystemBoardCode.text();
		
		var tdTranslation = tds.eq(3);
		var oldTranslation = tdTranslation.text();
		tdTranslation.text('');
		
		but.hide();
		
		var selectSystemBoards = $('select.selectSystemBoards')
			.clone()
			.removeClass()
			.appendTo(tdTranslation)
			.show()
			.val(oldSystemBoardCode)
			.keydown(function(event){
				switch(event.keyCode){
					case 13:
						funSubmitChangeMapping();
						break;
					case 27:
						funCancelChangeMapping();
						break;
				}
			})
			.focus()
		;
		
		var funSubmitChangeMapping = function(e){
			if(selectSystemBoards.val().length == 0)
			{
				return;
			}
			var vendorId = tds.eq(0).metadata().vendorId;
			$.ajax({
				url: '/a_admin/board.changeMapping',
				method: 'post',
				timeout: 20000,
				dataType: 'json',
				async: true,
				data: {
					systemCode: selectSystemBoards.val(),
					vendorId: vendorId,
					vendorCode: vendorBoardCode
				},
				success: function(data){
					if(data.status && data.status == 'success')
					{
						tdSystemBoardCode.removeAttr('style').text(data.data.code);
						tdTranslation.removeAttr('style').text(data.data.translation);
						funCloseChangeMapping();
					}
					else
					{
						alert('Translation saving error!');
					}							
				},
				error: function(){
					alert('Translation saving error!');
				}
			});
			funCloseChangeMapping();
		};
		
		var funCancelChangeMapping = function(e){
			tdTranslation.text(oldTranslation);
			funCloseChangeMapping();
		};
		
		var funCloseChangeMapping = function(e){
			selectSystemBoards.remove();
			tdBut.children().not('.butChange').remove();
			but.show();
		};
		
		$('<input class="button-ok" type="button" value="' + 'BTN_OK'.translate() + '" />')
			.appendTo(tdBut)
			.click(function(e){
				funSubmitChangeMapping();
			})
		;
		
		$('<input class="button-cancel" type="button" value="' + 'BTN_CANCEL'.translate() + '" />')
			.appendTo(tdBut)
			.click(function(){
				funCancelChangeMapping();
			})
		;
	});

//selectSystemBoards

/*
	$('#locationsList input[type="button"].butChange').click(function(e){
		var td = $(this).parents('tr').children();
		var tdt = td.eq(2);
		var tdb = td.eq(3);
		
		var val = tdt.text();
		tdt.html('');
		
		var inp = $('<input type="text" />');
		inp.val(val);
		inp.attr('title', val);
		inp.keydown(function(event){
			if(event.keyCode == 13)
			{
				funSaveTranslation();
			}
		});
		tdt.append(inp);
		inp.focus();
		
		
		var butEdit = $(this);
		butEdit.hide();

		var funCloseEditing = function(e){
			tdb.find('input[type="button"]').not('.butChange').remove();
			butEdit.show();
		};
		
		var funSaveTranslation = function(e){
			var newVal = inp.val().replace(/^[\s\t\r\n]+/g, '').replace(/[\s\t\r\n]+$/g, '');
			if(newVal.length > 0 && newVal != val)
			{
				var metaVal = td.eq(1).metadata();
				$.ajax({
					url: '/a_admin/board.saveTranslation',
					method: 'post',
					timeout: 20000,
					async: true,
					data: {
						vendorId: metaVal.vendorId,
						boardName: metaVal.name,
						boardNameTranslation: newVal,
						language: $('select[name="sTranslationLang"]').val()
					},
					success: function(data){
						if(data == 'success')
						{
							inp.remove();
							var tddd = tdb.parent().children().eq(2);
							tddd.removeAttr('style').text(newVal);
							funCloseEditing();
						}
						else
						{
							alert('Translation saving error!');
						}							
					},
					error: function(){
						alert('Translation saving error!');
					}
				});
			}
			else
			{
				alert('Please define a new translation before saving the board.');
				inp.focus();
			}
		};
		
		$('<input class="button-ok" type="button" value="' + 'BTN_OK'.translate() + '" />').click(funSaveTranslation).appendTo(tdb);
		
		$('<input class="button-cancel" type="button" value="' + 'BTN_CANCEL'.translate() + '" />').click(function(){
			var val = inp.attr('title');
			inp.remove();
			tdt.text(val);
			funCloseEditing();
		}).appendTo(tdb);
		
	});
*/
});
